package com.aqie.easy.matrix;

import java.util.ArrayDeque;

/**
 * 766 托普利兹矩阵
 * todo
 */
public class IsToeplitzMatrix {
    public boolean isToeplitzMatrix(int[][] matrix) {
        int R = matrix.length;
        int C = matrix[0].length;
        ArrayDeque<Integer> deque = new ArrayDeque<>();
        for (int c = 0; c < C; c++){
            deque.push(matrix[0][c]);
        }

        for (int i = 1; i < R; i++){
            for (int j = 1; j < C; j++){
                if (deque.peekFirst() != matrix[i][j]){
                    return false;
                }
            }
            deque.addFirst(matrix[i][0]);   // offerFirst
            deque.removeLast();             // pollLast
        }
        return true;
    }

    public boolean isToeplitzMatrix2(int[][] matrix) {
        int R = matrix.length;
        int C = matrix[0].length;
        for (int r = 0; r < R - 1; r++){
            for (int c = 0; c < C - 1; c++){
                if (matrix[r][c] != matrix[r + 1][c + 1]){
                    return false;
                }
            }
        }
        return true;
    }
}
